<?php
	include 'config.php';
	include 'utils.php';

	class SQL_UTILS_MESSAGE{
		const COULD_NOT_CONNECT_TO_SERVER = "Could not connect to server.";
		const COULD_NOT_SELECT_DATABASE = "Could not select database.";
	}

	class SqlUtils{
		protected $db_config = array();
		
		function __construct($db_config = array()) {
			if (count($db_config) == 0){
				global $GLOBALS;
				$db_config = $GLOBALS['DB'];
			}
			$this->db_config = $db_config;
		}
		public function getConnection()
		{
			if(!isset($this->g_link)){
				$this->g_link = mysql_connect( 
					$this->_fullHostName($this->db_config['HOST'], $this->db_config['PORT']),
					$this->db_config['USER'],
					$this->db_config['PASSWORD']
					) or die(SQL_UTILS_MESSAGE::COULD_NOT_CONNECT_TO_SERVER);
				mysql_select_db($this->db_config['DATABASE'], $this->g_link) or die(SQL_UTILS_MESSAGE::COULD_NOT_SELECT_DATABASE);
				debug_msg("<b>SQL connect to</b>: HOST = ".$this->db_config['HOST']." DB = ".$this->db_config['DATABASE']."<br/>");
				if (!$this->g_link) {
					debug_msg("<b>SQL connect error</b>: " . mysql_error() . "<br/>");
				}else{
					$sql = "charset utf8";
					mysql_query($sql,$this->g_link);
					$sql = "SET NAMES 'utf8'";
					mysql_query($sql,$this->g_link);
					mysql_set_charset('utf8',$this->g_link);
					debug_msg("<b>Set UTF8</b><br/>");
				}
			}
			return $this->g_link;
	    }
		public function closeConnection()
		{
			if( $this->g_link != false ){
				mysql_close($this->g_link);
			}
			$this->g_link = false; 
		}

		protected function _fullHostName($host, $port)
		{
			if (is_null($port) || ($port=='')) return $host;
			return $host . ":" . $port;
		}

		public function getRows($sql){
			$rows = array();
			$i=0;
			$rs = mysql_query($sql,$this->getConnection());
			if (mysql_error($this->getConnection())){
				debug_msg("<b>SQL error</b>: " . mysql_error($this->getConnection()) . "<br/>");
				debug_msg("<b>QUERY</b>: " . $sql . "<br/>");
				return false;
			}
			while($rsarr = mysql_fetch_array($rs)){
				$rows[$i] = $rsarr;
				$i++;
			}
			return $rows;
		}

		public function insertRows($colList,$rowList,$tbl){
			$numOfRow = count($rowList); // Number of row to insert
			$numOfCol = count($colList); // Number of col
			$sql = 'INSERT INTO '.$tbl.'(';
			for($i=0;$i<$numOfCol;$i++){
				$sql .= $colList[$i];
				if($i != $numOfCol-1){
					$sql .= ',';
				}
			}
			$sql .= ') VALUES';
			for($i=0;$i<$numOfRow;$i++){
				$sql .= '(';
				for($j=0;$j<$numOfCol;$j++){
					$sql .= $rowList[$i][$j];
					if($j != $numOfCol-1){
						$sql .= ',';
					}
				}
				$sql .= ')';
				if($i != $numOfRow-1){
					$sql .= ',';
				}
			}
			debug_msg('<b>Query</b>: '.$sql.' <br/>');
			mysql_query($sql,$this->getConnection());
			if (mysql_error($this->getConnection())){
				debug_msg("<b>SQL error</b>: " . mysql_error($this->getConnection()) . "<br/>");
				return false;
			}else{
				return true;
			}		
		}
		public function updateRows($colList,$rowList,$tbl,$tblId,$id){
			if(count($rowList) != count($colList)){
				return false;
			}
			else $num = count($rowList); //Number of row to update
			$sql = 'UPDATE '.$tbl.' SET ';
			for($i=0; $i<$num; $i++){
				$sql .= $colList[$i];
				$sql .= ' = ';
				$sql .= $rowList[$i];
				if($i != $num-1){
					$sql .= ' , ';
				}
			}
			$sql .= ' WHERE '.$tblId.' = '.$id;
			debug_msg('<b>Query</b>: '.$sql.' <br/>');
			mysql_query($sql,$this->getConnection());
			if (mysql_error($this->getConnection())){
				debug_msg('<b>SQL error</b>: ' . mysql_error($this->getConnection()) . '<br/>');
				return 'FALSE'.$sql;
			}else{
				return TRUE.$sql;
			}
		}
	}
?>